import requests
from bs4 import BeautifulSoup
import time

header = {
    "user-agent":
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36"
}


def down1():
    for i in range(1, 3):
        url = "https://www.ymh1234.com/comic/8942/1894557.html?p=" + str(i)
        print(url)
        down2(url)


def down2(url):
    r = requests.get(url, headers=header)
    r.encoding = "utf-8"
    print(r.status_code)
    demo = r.text
    down3(demo)


def down3(url):
    soup = BeautifulSoup(url, "html.parser")
    tags = soup.find_all('img') #  id='images'#
    print(len(tags))  # 查看标签数量#
    for tag in tags:
        image = tag["src"]
        print(image)
        down4(image)


def down4(shuchu):
    print(time.time())
    filename = "./image/" + str(int(time.time() * 1000)) + ".jpg"
    r = requests.get(shuchu, headers=header)
    f = open(filename, "wb")
    f.write(r.content)
    f.close()


if __name__ == '__main__':
    down1()
